package gameFile;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.Color;

import gameFile.GamePlay.*;

abstract public class LightBike {
	private int playerId;
    private int xPosition;
    private int yPosition;
    private int speed;
    protected Action action;
    public Direction direction;
    private boolean isAlive = true;
    protected Color bikeColor,wallColor;
    public int distance;
    
    private int turnX, turnY;

    public LightBike()  {
    	this.playerId = 0;
    	this.xPosition = 0;
    	this.yPosition = 0;
    	this.speed = 1;
    	this.direction = Direction.DOWN;
    	this.turnX = this.xPosition;
    	this.turnY = this.yPosition;
    	this.action = Action.GOSTRAIGHT;
    }
    
    public LightBike(int ID, int x, int y, int speed, Direction direction) {
    	this.playerId = ID;
        this.xPosition = turnX = x;
        this.yPosition = turnY = y;
        this.speed = speed;
        this.direction = direction;
        this.turnX = this.xPosition;
    	this.turnY = this.yPosition;
    	this.action = Action.GOSTRAIGHT;
    }

    
    public void setSpeed(int s){
    	this.speed=s++;
    	System.out.println(speed);
    }
    public int getSpeed(){
    	return this.speed;
    }
    
    public int getXPosition(){
        return xPosition;
    }
    
    public int getYPosition(){
        return yPosition;
    }
    
    public Direction getDirection(){
    	return this.direction;
    }
    
    public Color getWallColor(){
    	return this.wallColor;
    }
    
    public WallSegment playTurn() {
		moveBike();
		WallSegment wall = null;
		wall = this.getTrailingWall();
		switch(action){
		
		case GOLEFT:
		//	wall = this.getTrailingWall();
			this.moveLeft();
			break;
		case GORIGHT:
		//	wall = this.getTrailingWall();
			this.moveRight();
			break;
		
		case GOSTRAIGHT:
		//	wall = this.getTrailingWall();
			break;
		}
		action = Action.GOSTRAIGHT;
		
		return wall;
	}
    public void setAction(Action act)
	{
		this.action = act;
	}
    
    public void drawBike(Graphics g){
    	g.setColor(bikeColor);
    	g.drawString(Integer.toString(distance), xPosition, yPosition);
		g.fillOval(xPosition-4, yPosition-4, 8, 8);
    }
    
    public void moveBike(){
    	switch(direction) {
    	case UP:
    		yPosition=yPosition-speed;
    		break;  
    	case LEFT:
    		xPosition=xPosition-speed;
    		break;
    	case DOWN:
    		yPosition=yPosition+speed;
    		break; 
    	case RIGHT:
    		xPosition=xPosition+speed;
    		break;    	   	  	
    	}
    }
    
	public void moveLeft(){
    	switch(direction) {
    	case UP:
    		direction = Direction.LEFT;
    		break;  
    	case LEFT:
    		direction = Direction.DOWN;
    		break;
    	case DOWN:
    		direction = Direction.RIGHT;
    		break; 
    	case RIGHT:
    		direction = Direction.UP;
    		break;    	   	  	
    	}
    	
    	turnX = xPosition;
    	turnY = yPosition;
    }
    
    public void moveRight(){
    	switch(direction) {
    	case UP:
    		direction = Direction.RIGHT;
    		break;  
    	case LEFT:
    		direction = Direction.UP;
    		break;
    	case DOWN:
    		direction = Direction.LEFT;
    		break;
    	case RIGHT:
    		direction = Direction.DOWN;
    		break;
    	}
    	turnX = xPosition;
    	turnY = yPosition;
    }
    
    public WallSegment getTrailingWall() {
    	int length = 0;
    	
    	
    	if (turnX - xPosition != 0) length =  xPosition - turnX; 
    	else if (turnY - yPosition != 0) length = yPosition - turnY;
    	//else System.out.println("OH NO!");
    	
    	return new WallSegment(playerId, turnX, turnY, length,direction,wallColor);
    }
    
    public void drawWall(Graphics g) {
    	getTrailingWall().drawWall(g);
    }
    
    public int getID(){
    	return playerId;
    }

    public void playerDeath() {
    	isAlive = false;
    }
    
	public boolean isAlive() {
		return isAlive;
	}

	public void moveBikeWithMouse(MouseEvent e) {
		if (e.getX() < .5*GraphicPage.Width){
			setAction(Action.GOLEFT);
		}
		if (e.getX() >= .5*GraphicPage.Width){
			setAction(Action.GORIGHT);
		};
	}
	
	public void cheats(int k){
		this.setSpeed(k);
		
		
	}

}


